<template>
  <div class="topbar">
    <!-- 版心 -->
    <div class="wrap topbarwrap">
      <div class="left">欢迎来到叩丁狼积分商城</div>
      <ul class="right">
        <li>
          <img :src="userInfo.headImg" alt="" />
          用户名：{{ userInfo.nickName }}
        </li>
        <li>我的积分：{{ userInfo.coin }}</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>
        <li class="cart-btn" v-if="isLoginStatus">
          <img src="../assets/img/cart.png" alt="" />
          <span>购物车</span>
          <strong>{{ cartTotal }}</strong>
        </li>
        <li class="loginbtn" @click="changeIsShowLoginModal(true)" v-else>
          登录
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { mapMutations, mapState, mapActions } from "vuex";
import { WeiXinLoginAPI } from "@/request/api";
export default {
  created() {
    this.$router.onReady(async () => {
      let paramscode = this.$route.query.code;
      if (paramscode) {
        let res = await WeiXinLoginAPI({
          code: paramscode,
        });
        console.log(res);
        if (res.code == 0) {
          alert("登录成功");
          localStorage.setItem("x-auth-token", res["x-auth-token"]);
          this.changeLoginStatus(true);
          this.$router.push(this.$route.path);
          this.asyncchangeUserInfo();
        } else if (res.code == 400) {
          alert("请重新扫码！！！");
          this.changeIsShowLoginModal(true);
        } else if (res.code == 407) {
          alert("请使用手机号再登录一次！");
          
          this.changeIsShowLoginModal(true);
          localStorage.setItem("uuid", res.uuid);
          this.$router.push(this.$route.path);
        }
      } else {
        let token = localStorage.getItem("x-auth-token");
        this.changeLoginStatus(Boolean(token));

        if (token) {
          this.asyncchangeUserInfo();
        } else {
          this.initUserInfo();
        }
      }
    });
  },
  computed: {
    ...mapState({
      isLoginStatus: (state) => state.loginStatus.isLoginStatus,
      userInfo: (state) => state.userInfo.userInfo,
      cartTotal: (state) => state.userInfo.cartTotal,
    }),
  },
  methods: {
    ...mapMutations({
      //当前组件的方法名:"模块名/方法名"
      changeIsShowLoginModal: "showModal/changeIsShowLoginModal",
      changeLoginStatus: "loginStatus/changeLoginStatus",
      initUserInfo: "userInfo/initUserInfo",
    }),
    ...mapActions({
      asyncchangeUserInfo: "userInfo/asyncchangeUserInfo",
    }),
  },
};
</script>

<style lang="less" scoped>
.topbar {
  height: 40px;
  line-height: 40px;
  background: #242b39;
  font-size: 14px;
  font-family: SourceHanSansSC;
  font-weight: 300;
  color: #fffefe;
  .topbarwrap {
    display: flex;
    justify-content: space-between;
    .right {
      li {
        float: left;
        padding: 0 10px;
        text-align: center;
        cursor: pointer;
        img {
          width: 23px;
          height: 22px;
          border-radius: 13px;
          vertical-align: middle;
        }
        &.loginbtn {
          width: 124px;
          height: 40px;
          padding: 0;
          background: #0a328e;
        }
        &.cart-btn {
          width: 124px;
          height: 40px;
          background: #0a328e;
          text-align: center;
          img {
            width: 20px;
            height: 19px;
            vertical-align: middle;
          }
          span {
            margin: 0 8px;
          }
          strong {
            width: 22px;
            height: 22px;
            line-height: 22px;
            display: inline-block;
            border-radius: 50%;
            background-color: #fd604d;
          }
        }
      }
    }
  }
}
</style>